/*   Valhalla MailChecker -- Simple IMAP Mail Checker
*    Copyright (C) 2010-2011  Guillaume Florimond (gflorimond at gmail dot com)
*
*    This program is free software: you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation, either version 3 of the License, or
*    any later version.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License
*    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/*
 * CheckMessagesFrame.java
 *
 * Created on 21 mai 2010, 15:42:10
 */

package fr.valhalla.mailcheck.gui.classic;

import javax.swing.DefaultListModel;

/**
 *
 * @author guillaume
 */
public class CheckMessagesFrame extends javax.swing.JFrame {

    CheckPane parent;
    DefaultListModel model = new DefaultListModel();
    int allMessages = 0;
    int newMessages = 0;

    /** Creates new form CheckMessagesFrame */
    public CheckMessagesFrame(CheckPane parent) {
        this.parent = parent;
        initComponents();
    }

    public void addToList(String item) {
        //model.addElement(item); // insérer à la fin de la liste
        model.add(0, item); // insérer au début de la liste
    }

    public void setAccountName(String account) {
        accountLabel.setText(account);
        this.setTitle(account);
    }

    public void setLastUpdate(String updateTime) {
        lastUpdateLabel.setText("Dernière mise à jour : " + updateTime);
    }

    public void setNextUpdate(String updateTime) {
        nextUpdateLabel.setText("Prochaine mise à jour : " + updateTime);
    }

    public void setAllMessagesCount(int messages) {
        allMessagesLabel.setText(messages + " messages");
        allMessages = messages;
    }

    public void setNewMessagesCount(int messages) {
        newMessagesLabel.setText(messages + " nouveaux messages");
        newMessages = messages;
    }

    public void setProgress(int current, int total) {
        dlBar.setMaximum(total);
        dlBar.setValue(current);
        dlBar.setString("Loading messages (" + current + " / " + total + ")");
        dlBar.setStringPainted(true);
    }

    public void removeProgressBar() {
        this.remove(dlBar);
        this.repaint();
    }




    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        accountLabel = new javax.swing.JLabel();
        allMessagesLabel = new javax.swing.JLabel();
        nextUpdateLabel = new javax.swing.JLabel();
        newMessagesLabel = new javax.swing.JLabel();
        lastUpdateLabel = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        messagesList = new javax.swing.JList();
        dlBar = new javax.swing.JProgressBar();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });

        accountLabel.setFont(new java.awt.Font("Lucida Grande", 1, 18));
        accountLabel.setText("[accountName]");

        allMessagesLabel.setText("[allMessagesCount]");

        nextUpdateLabel.setText("[nextUpdate]");

        newMessagesLabel.setText("[newMessagesCount]");

        lastUpdateLabel.setText("[lastUpdate]");

        messagesList.setModel(model);
        messagesList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                messagesListMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(messagesList);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, dlBar, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 753, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 753, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, allMessagesLabel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 753, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, accountLabel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 753, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, nextUpdateLabel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 753, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, newMessagesLabel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 753, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, lastUpdateLabel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 753, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(accountLabel)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(allMessagesLabel)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(newMessagesLabel)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 315, Short.MAX_VALUE)
                .add(1, 1, 1)
                .add(dlBar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(lastUpdateLabel)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(nextUpdateLabel)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
        parent.setDetailsButtonSelected(false);
    }//GEN-LAST:event_formWindowClosed

    private void messagesListMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_messagesListMouseClicked
       if(evt.getClickCount() == 2) {
           int msgId = messagesList.getModel().getSize() - messagesList.getSelectedIndex() - 1;
           MessageFrame mf = new MessageFrame(parent.getChecker().getConnection(), msgId);
       }
    }//GEN-LAST:event_messagesListMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel accountLabel;
    private javax.swing.JLabel allMessagesLabel;
    private javax.swing.JProgressBar dlBar;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lastUpdateLabel;
    private javax.swing.JList messagesList;
    private javax.swing.JLabel newMessagesLabel;
    private javax.swing.JLabel nextUpdateLabel;
    // End of variables declaration//GEN-END:variables

}
